﻿// <copyright file="MovieReviewsByReviewer.cs" company="Microsoft Corporation">
// Copyright (c) 2009 Microsoft Corporation All Rights Reserved
// </copyright>
// <author>Michael S. Scherotter</author>
// <email>mischero@microsoft.com</email>
// <date>2009-04-08</date>
// <summary>Movie Reviews by Reviewer CLR Class</summary>

namespace NYTimes
{
    using System;
    using System.ComponentModel;
    using System.Globalization;
    
    using System.Text;
#if SILVERLIGHT
#else
    using System.Web;
#endif
#if WINDOWS_PHONE
#elif SILVERLIGHT
    using System.Windows.Browser;
#endif
    using NYTimes.Core;
    using NYTimes.Data;
    
    /// <summary>
    /// New York Times Movie Reviews By Reviewer API
    /// </summary>
    /// <remarks>http://developer.nytimes.com/docs/movie_reviews_api#h3-reviews-reviewers</remarks>
    public class MovieReviewsByReviewer : MovieReviewBase
    {
        #region Fields
        /// <summary>
        /// the reviewer name
        /// </summary>
        private string reviewerName;

        #endregion

        /// <summary>
        /// Initializes a new instance of the MovieReviewsByReviewer class.
        /// </summary>
        public MovieReviewsByReviewer()
        {
            this.Order = MovieReviewOrder.PublicationDate;
        }

        #region Properties
        /// <summary>
        /// Gets or sets the reviewer name
        /// </summary>
        [Description("The reviewer's name to filter by.")]
        public string ReviewerName 
        { 
            get
            {
                return this.reviewerName;
            } 

            set
            {
                this.reviewerName = value;

                this.UpdateResults();
            }
        }

        /// <summary>
        /// Gets or sets the critics picks
        /// </summary>
        [Description("Only return critic's picks.")]
        public bool? CriticsPick { get; set; }

        /// <summary>
        /// Gets or sets the order
        /// </summary>
        [Description("Specify the order of the results.")]
        public MovieReviewOrder Order { get; set; }

        #endregion

        #region Methods
        /// <summary>
        /// Generate the REST URI
        /// </summary>
        /// <returns>the rest URI or null</returns>
        public override Uri GenerateUri()
        {
            var builder = new StringBuilder("http://api.nytimes.com/svc/movies/v2/reviews/reviewer/");
            
            if (string.IsNullOrEmpty(this.ReviewerName))
            {
                return null;
            }

            builder.AppendFormat(
                CultureInfo.InvariantCulture, 
                "{0}.xml?api-key={1}",
                Uri.EscapeDataString(this.ReviewerName),
                this.ApiKey);

            builder.AppendFormat(
                CultureInfo.InvariantCulture,
                "&order={0}",
                MovieReviewBase.ReviewOrderMap[this.Order]);

            this.AppendOffset(builder);

            AppendLimiter(builder, this.CriticsPick, "critics-pick");

            return new Uri(builder.ToString(), UriKind.Absolute);
        }
        #endregion
    }
}
